//
//  CinemaViewController.m
//  MyMovie
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "CinemaViewController.h"
#import "District.h"
#import "Cinema.h"
#import "CinemaTableViewCell.h"

@interface CinemaViewController ()<UITableViewDelegate, UITableViewDataSource>

{
    NSMutableArray *districtArray;
    NSMutableArray *cinemaArray;

}
@property (strong, nonatomic) IBOutlet UITableView *cinemaTableView;
@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData];

}

- (void)loadData {

    districtArray = [[NSMutableArray alloc] init];

    NSDictionary *dic = [JSONDataService loadJSONFielWithName:@"district_list"];
    for (NSDictionary *d in dic[@"districtList"]) {

        District *district = [[District alloc] initWithDictionary:d];
        [districtArray addObject:district];
    }

    dic = [JSONDataService loadJSONFielWithName:@"cinema_list"];
    for (NSDictionary *d in dic[@"cinemaList"]) {

        Cinema *cinema = [[Cinema alloc] initWithDictionary:d];
        for (District *district in districtArray) {
            if ([cinema.districtID isEqualToString:district.districtID]) {
                [district.cinemaArray addObject:cinema];
                break;
            }
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return districtArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    District *district = districtArray[section];
    if (district.isOpen == NO) {
        return 0;
    }
    return district.cinemaArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CinemaCell"];

    District *district = districtArray[indexPath.section];
    cell.cinema = district.cinemaArray[indexPath.row];


    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 35;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"hotMovieBottomImage"] forState:UIControlStateNormal];
    District *district = districtArray[section];
    [button setTitle:district.districtName forState:UIControlStateNormal];
    button.tag = section;

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];


    return button;

}

- (void)buttonAction:(UIButton *)button {

    District *district = districtArray[button.tag];
    district.isOpen = !district.isOpen;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:button.tag];
    [_cinemaTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    
}






@end
